package com.mmall.controller.portal;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.common.TokenCache;
import com.mmall.pojo.User;
import com.mmall.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Create by lisy on Qingdao
 **/
@Controller
@RequestMapping("/user/")
public class UserController {

    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */

    @Autowired
    private IUserService iUserService;

    @RequestMapping(value="login.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){
        //service-->mybatis-->dao
        ServerResponse serverResponse = iUserService.loginService(username, password);
        if(serverResponse.isSuccess()){
            session.setAttribute(Const.CURRENT_USER, serverResponse.getData());
        }
        return serverResponse;
    }

    @RequestMapping(value="register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register(User user){
        return iUserService.registerService(user);
    }

    @RequestMapping(value="logout.do", method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess();
    }

    @RequestMapping(value="check_valid.do", method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkUser(String str, String type){
        return iUserService.checkValid(str, type);
    }

    @RequestMapping(value="get_user_info.do", method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getUserInfo(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("没有取到user");
        }
        return ServerResponse.createBySuccess(user);
    }

    @RequestMapping(value="forget_get_question.do", method=RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> getQuestion(String username){
        return iUserService.getQuestionByUsername(username);
    }

    @RequestMapping(value="forget_check_answer.do", method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetCheckAnswer(String username, String question, String answer){
        return iUserService.checkAswer(username, question, answer);
    }

    /**
     * ####7.忘记密码的重设密码 /user/forget_reset_password.do
     * localhost:8080/user/forget_reset_password.do?username=aaa&passwordNew=xxx&forgetToken=531ef4b4-9663-4e6d-9a20-fb56367446a5
     */
    @RequestMapping(value="forget_reset_password.do", method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String username, String passwordNew, String forgetToken){
        return iUserService.forgetResetPassword(username, passwordNew, forgetToken);
    }

    /**
     * ####8.登录中状态重置密码 /user/reset_password.do
     */
    @RequestMapping(value="reset_password.do", method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(String passwordOld, String passwordNew, HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorMssage("用户未登录");
        }
        return iUserService.resetPassword(passwordOld, passwordNew, user);
    }

    /**
     * ####9.登录状态更新个人信息 /user/update_information.do
     */
    @RequestMapping(value="update_information.do", method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> updateInformation(HttpSession session, User updateUser){
        //判断当前session中是否为空
        User currentUser = (User)session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createByErrorMssage("请登录");
        }

        updateUser.setId(currentUser.getId());
        updateUser.setUsername(currentUser.getUsername());
        ServerResponse<User> response = iUserService.updateInformation(updateUser);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER, response.getData());
        }
        return response;
    }

    /**
     * ####10.获取当前登录用户的详细信息，并强制登录 /user/get_information.do
     */
    @RequestMapping(value="get_information.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getInformation(HttpSession session){
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createByErrorMssage(ResponseCode.NEED_LOGIN.getCode(), "未登录，需要强制登录");
        }
        ServerResponse response = iUserService.getInformation(currentUser.getId());
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER, response.getData());
        }
        return response;
    }

}
